﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;

using Husb.Data;
using Husb.DataUtil;

using GroundWater.Data;
using WellTable = GroundWater.Data.InfoDataSet.WellsDataTable;
using WellRow = GroundWater.Data.InfoDataSet.WellsRow;

namespace GroundWater.DataAccess
{
    public class WellDataAdapter : DataAccessAdapter<InfoDataSet, WellTable, WellRow>
    {
        public WellDataAdapter()
        {
            this.TableName = "Wells";

            GetAllStoredProc = SqlStatements.Well_FetchAll;
            InsertStoredProc = SqlStatements.Well_Insert;
            UpdateStoredProc = SqlStatements.Well_Update;
            DeleteStoredProc = SqlStatements.Well_Delete;

            this.GetByIdStoredProc = SqlStatements.Well_FetchById;
        }

        protected override void PopulateInsertParameters(Database db, DbCommand cmd)
        {
            db.AddInParameter(cmd, "Code", DbType.String, "Code", DataRowVersion.Current);
            PopulateParameters(db, cmd);
            //Code, :Name, :WaterSource, :Longitude, :Latitude, 
            //:Landform, :ResourceRegionCode, :AdministrativeDivisionsCode, :FunctionRegionCode, :Diameter, 
            //:Depth, :ShaftSinkingDate, :ProjectRuningState, :PumpType, :DesignYearlyWaterQuantity, 
            //:DesignFlowRate, :StaticLevel, :DynamicLevel, :RemoveEquipment, :WaterQuality, 
            //:Administration, :SectionPlane, :SIMCard, :BTUAddress, :WaterLevel, 
            //:GatheringDate
        }

        protected override void PopulateUpdateParameters(Database db, DbCommand cmd)
        {
            //base.PopulateUpdateParameters(db, cmd);
            PopulateParameters(db, cmd);
            db.AddInParameter(cmd, "Code", DbType.String, "Code", DataRowVersion.Current);
        }

        private static void PopulateParameters(Database db, DbCommand cmd)
        {
            //db.AddInParameter(cmd, "Code", DbType.String, "Code", DataRowVersion.Current);
            db.AddInParameter(cmd, "Name", DbType.String, "Name", DataRowVersion.Current);
            db.AddInParameter(cmd, "WaterSource", DbType.String, "WaterSource", DataRowVersion.Current);
            db.AddInParameter(cmd, "Longitude", DbType.Decimal, "Longitude", DataRowVersion.Current);
            db.AddInParameter(cmd, "Latitude", DbType.Decimal, "Latitude", DataRowVersion.Current);

            db.AddInParameter(cmd, "Landform", DbType.String, "Landform", DataRowVersion.Current);
            db.AddInParameter(cmd, "ResourceRegionCode", DbType.String, "ResourceRegionCode", DataRowVersion.Current);
            db.AddInParameter(cmd, "AdministrativeDivisionsCode", DbType.String, "AdministrativeDivisionsCode", DataRowVersion.Current);
            db.AddInParameter(cmd, "FunctionRegionCode", DbType.String, "FunctionRegionCode", DataRowVersion.Current);
            db.AddInParameter(cmd, "Diameter", DbType.Decimal, "Diameter", DataRowVersion.Current);

            db.AddInParameter(cmd, "Depth", DbType.Decimal, "Depth", DataRowVersion.Current);
            db.AddInParameter(cmd, "ShaftSinkingDate", DbType.DateTime, "ShaftSinkingDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "ProjectRuningState", DbType.String, "ProjectRuningState", DataRowVersion.Current);
            db.AddInParameter(cmd, "PumpType", DbType.String, "PumpType", DataRowVersion.Current);
            db.AddInParameter(cmd, "DesignYearlyWaterQuantity", DbType.Decimal, "DesignYearlyWaterQuantity", DataRowVersion.Current);

            db.AddInParameter(cmd, "DesignFlowRate", DbType.Decimal, "DesignFlowRate", DataRowVersion.Current);
            db.AddInParameter(cmd, "StaticLevel", DbType.Decimal, "StaticLevel", DataRowVersion.Current);
            db.AddInParameter(cmd, "DynamicLevel", DbType.Decimal, "DynamicLevel", DataRowVersion.Current);
            db.AddInParameter(cmd, "RemoveEquipment", DbType.String, "RemoveEquipment", DataRowVersion.Current);
            db.AddInParameter(cmd, "WaterQuality", DbType.String, "WaterQuality", DataRowVersion.Current);

            db.AddInParameter(cmd, "Administration", DbType.String, "Administration", DataRowVersion.Current);
            db.AddInParameter(cmd, "SectionPlane", DbType.String, "SectionPlane", DataRowVersion.Current);
            db.AddInParameter(cmd, "SIMCard", DbType.String, "SIMCard", DataRowVersion.Current);
            db.AddInParameter(cmd, "BTUAddress", DbType.String, "BTUAddress", DataRowVersion.Current);
            db.AddInParameter(cmd, "WaterLevel", DbType.String, "WaterLevel", DataRowVersion.Current);

            db.AddInParameter(cmd, "GatheringDate", DbType.String, "GatheringDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "Altitude", DbType.Decimal, "Altitude", DataRowVersion.Current);
            db.AddInParameter(cmd, "SequenceNumber", DbType.Int32, "SequenceNumber", DataRowVersion.Current);
            db.AddInParameter(cmd, "MinimumWaterlevel", DbType.Decimal, "MinimumWaterlevel", DataRowVersion.Current);
            db.AddInParameter(cmd, "MaximumWaterlevel", DbType.Decimal, "MaximumWaterlevel", DataRowVersion.Current);
            
        }

        protected override void PopulateDeleteParameters(Database db, DbCommand cmd)
        {
            //base.PopulateDeleteParameters(db, cmd);
            db.AddInParameter(cmd, "Code", DbType.String, "Code", DataRowVersion.Original);
        }

        protected override void PopulateStringIdParameters(Database db, DbCommand cmd, string id)
        {
            //base.PopulateStringIdParameters(db, cmd, id);
            //db.AddInParameter(cmd, "Code", DbType.String, "Code", DataRowVersion.Original);
            db.AddInParameter(cmd, "Code", DbType.String, id);// DbType.AnsiString
        }

        //public override InfoDataSet.WellsRow GetById(Guid id, bool isStoredProc)
        //{
        //   WellTable wells =  GetTable(GetByIdStoredProc, new List<DatabaseParameter>(), isStoredProc, false, null);
        //   if (wells != null && wells.Count > 0)
        //       return wells[0];
        //   else
        //       return null;
        //}
    }
}
